package nongchang.kuaijiecaifu.com.ncapp_android.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;


import java.util.Calendar;
import java.util.Date;

import ezy.lite.util.DateTime;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.util.ScaleUtil;


/**
 * Created by Administrator on 2016/4/1 0001.
 */
public class ProgressView extends View {
    int windowWidth;
    int windowHeight;
    int mwidth;
    int mheight;
    int fristColor;
    int secondColor;
    private int progress;
    private Context context;
    private float prodp;
    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        windowWidth = getWindowWidth(context);
        windowHeight = getWindowHeight(context);
        prodp=ScaleUtil.dip2px(context,10);
        final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
        try {
            fristColor = attributes.getColor(R.styleable.ProgressView_first_color, Color.parseColor("#4f4f4f"));
            secondColor = attributes.getColor(R.styleable.ProgressView_second_color, Color.parseColor("#8b8b8b"));
            progress = attributes.getInt(R.styleable.ProgressView_progress, 0);
        } catch (Exception e) {

        } finally {
            attributes.recycle();
        }
    }

    public int getWindowWidth(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    public int getWindowHeight(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);

        mwidth = windowWidth / 10;
        mheight = windowHeight / 10;
        if (widthmode == MeasureSpec.AT_MOST && heightmode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mwidth, mheight);
        } else if (widthmode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mwidth, heightsize);
        } else if (heightmode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthsize, mheight);
        } else {
            setMeasuredDimension(widthsize, heightsize);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        setBackgroundColor(secondColor);
        int width=getMeasuredWidth();
        int height=getMeasuredHeight();
        Paint paint = new Paint();
        paint.setColor(fristColor);
        paint.setStyle(Paint.Style.FILL);//实心矩形框
//        Path path = new Path();
//        path.moveTo(0, 0);// 此点为多边形的起点
//        path.lineTo(0,height );
//        path.lineTo((float) width/progress/100,height);
//        if((float) width/progress/100-prodp<prodp){
//            path.lineTo(0 ,0);
//        }else {
//            path.lineTo((float) width / progress / 100 - prodp, 0);
//        }
//        path.close(); // 使这些点构成封闭的多边形
//        canvas.drawPath(path, paint);


        Path path=new Path();
        path.moveTo(10, 330);
        path.lineTo(70,330);
        path.lineTo(40,270);
        path.close();
        canvas.drawPath(path, paint);
    }

    public void setProgress(int pro) {
        progress = pro;
        invalidate();
    }

}
